package polymorphic;

/**
 * @Package: polymorphic
 * @ClassName: PolymorphicArrary
 * @Author: x2606
 * @CreateTime: 2022/2/25 23:08
 * @Description: 多态数组
 */

public class PolymorphicArrary {
    public static void main(String[] args) {

        Person[] person = new Person[5];
        person[0]=new Person("A",20);
        person[1]=new Student("B",21,90);
        person[2]=new Student("C",22,89);
        person[3]=new Teacher("D",23,5000);
        person[4]=new Teacher("E",24,8000);

//        for(Person i:person){
//            i.say();
//        }
        for (int i = 0; i < person.length; i++) {
            System.out.println(person[i].say());
            //向下转型调用子类特有方法
            if (person[i] instanceof Student ){
               ((Student) person[i]).study();
            }else if (person[i] instanceof Teacher){
                ((Teacher) person[i]).teach();
            }else {
                System.out.println("您的对象类型错误！");
            }
        }

    }
}

class Person{
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String say(){
        return this.name+"\t"+this.age;
    }
}

class Student extends Person{
    private double score;

    public Student(String name, int age,double score) {
        super(name, age);
        this.score=score;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    @Override
    public String say() {
        return super.say()+"\t"+score;
    }

    public void study(){
        System.out.println(getName()+" studying");
    }
}

class Teacher  extends Person{
    private double salary;

    public Teacher(String name, int age, double salary) {
        super(name, age);
        this.salary = salary;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    @Override
    public String say() {
        return super.say()+"\t"+salary;
    }

    public void teach(){
        System.out.println(getName()+" teaching");
    }
}
